$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/hotBank/list',
        datatype: "json",
        colModel: [
            { label: 'loanId', name: 'bankId', index: 'bankId', width: 50, key: true ,hidden:true},
            { label: '银行名称', name: 'bankName', index: 'bankName', width: 50 },
            { label: 'icon', name: 'bankIcon', index: 'bankIcon', width: 50 ,formatter:function (value,options,rowObj) {
                return '<img src='+value+' style="width:30px;height:30px;">';
            }},
            { label: 'banner', name: 'bankHot', index: 'bankHot', width: 50 ,formatter:function (value,options,rowObj) {
                if (value == '0'){
                    return '<span class="label label-warning">非推荐</span>';;
                }else if (value == '1'){
                    return '<span class="label label-success">推荐</span>';;
                }
            }},
            { label: '链接地址', name: 'bankUrl', index: 'bankUrl', width: 80 },
            // { label: '申请额度', name: 'loanMoney', index: 'loanMoney', width: 50 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条\
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    document.getElementById("iconImage").style.height = '70px';
    $("#iconImage").click(function () {
        $("#iconInput").click();
    });

    $("#iconInput").change(function () {
        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
        if (objUrl != null) {
            $("#iconImage").attr("src", objUrl);
            vm.hotBank.bankIcon = objUrl;
        }
    });

});

function getObjectURL(file) {
    var url = null;
    // 下面函数执行的效果是一样的，只是需要针对不同的浏览器执行不同的 js 函数而已
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

var vm = new Vue({
    el:"#app",
    data:{
        showList:true,
        title:'',
        hotBank:{}
    },
    methods:{
        update:function () {
            var ids = getSelectedRows();

            if (ids.length > 1){
                alert('不支持批量操作');
                return;
            }else if(ids.length == 0){
                alert('请选择一条数据');
                return;
            }
            vm.showList = false;
            $.ajax({
                url:'/platform/manage/hotBank/info/'+ids[0],
                dataType:'JSON',
                success:function (result) {
                    if (result.code == 200){
                        vm.hotBank = result.result;
                    }else {
                        alert(result.message,function () {
                            vm.reload();
                        });
                    }
                },
                error:function (e) {
                    console.error("error",e);
                    alert("请求失败",function () {
                        vm.reload();
                    });
                }
            });
        },
        addHot:function () {
            var ids = getSelectedRows();

            if (ids.length > 1){
                alert('不支持批量操作');
                return;
            }else if(ids.length == 0){
                alert('请选择一条数据');
                return;
            }
            window.confirm("添加到推荐？",function () {
                $.ajax({
                    url:"/platform/manage/hotBank/addHot/"+ids[0],
                    dataType:"JSON",
                    success:function (result) {
                        if (result.code == 200){
                            alert("操作成功");
                        }else {
                            alert(result.message);
                        }
                        vm.reload();
                    },
                    error:function (e) {
                        console.error("error",e);
                        alert("请求失败");
                    }
                })
            });
        },
        removeHot:function () {
            var ids = getSelectedRows();

            if (ids.length > 1){
                alert('不支持批量操作');
                return;
            }else if(ids.length == 0){
                alert('请选择一条数据');
                return;
            }
            window.confirm("从推荐移除？",function () {
                $.ajax({
                    url:"/platform/manage/hotBank/removeHot/"+ids[0],
                    dataType:"JSON",
                    success:function (result) {
                        if (result.code == 200){
                            alert("操作成功");
                        }else {
                            alert(result.message);
                        }
                        vm.reload();
                    },
                    error:function (e) {
                        console.error("error",e);
                        alert("请求失败");
                    }
                })
            });
        },
        del:function () {
            var ids = getSelectedRows();
            if (ids.length > 1){
                alert('不支持批量操作');
                return;
            }else if(ids.length == 0){
                alert('请选择一条数据');
                return;
            }
            window.confirm("确定要删除这条数据么？",function () {
                $.ajax({
                    url:'/platform/manage/hotBank/delete/'+ids[0],
                    dataType:'JSON',
                    success:function (result) {
                        if (result.code == 200){
                            alert("操作成功");
                            vm.reload();
                        }else {
                            alert(result.message);
                        }
                    },
                    error:function (e) {
                        console.error("error",e);
                        alert("请求失败");
                    }
                });
            });
        },
        add:function () {
            vm.hotBank = {};
            vm.showList = false;
        },
        saveOrUpdate:function () {
            Vue.delete(vm.hotBank,"bankIcon");
            var urlStr = vm.hotBank.bankId == null ? "/platform/manage/hotBank/save" : "/platform/manage/hotBank/update";
            $.ajaxFileUpload({
                url: urlStr,
                data: JSON.parse(JSON.stringify(vm.hotBank)),
                type: "POST",
                contentType: "multipart/form-data",
                secureuri: false,
                fileElementId: 'iconInput',
                dataType: 'json',
                success: function (result) {
                    if (result.code == 200) {
                        vm.reload();
                        alert("操作成功");
                    } else {
                        alert(result.message);
                        if (vm.hotBank.bankId == null) {
                            $("#iconImage").attr("src", "");
                        }
                    }
                },
                complete: function () {
                    $("#photo").replaceWith('<input type="file" style="display: none;" accept="image/*" name="bankIcon" id="iconInput" class="form-control" placeholder="file" />');
                    $("#photo").change(function () {
                        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
                        if (objUrl != null) {
                            $("#iconImage").attr("src", objUrl);
                            vm.hotBank.bankIcon = objUrl;
                        }
                    });
                },
                error: function (r,xml,states,e) {
                    console.log("error", e);
                    alert("请求失败，请重试");
                }
            });
        },
        reload:function () {
            vm.showList = true;
            vm.title = '';
            $("#jqGrid").jqGrid().trigger('reloadGrid');
        }
    }
});